<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\iocc\bindings;

#use \BadMethodCallException;
#use tehframework\iocc\Binding;
#use tehframework\iocc\ObjectDefinition;
#use tehframework\iocc\MethodCall;

class ConfigurableBinding extends Binding
{
	/**
	 * @var ObjectDefinition
	 */
	private $definition;
	
	/**
	 * @var bool
	 */
	private $constructed;
	
	/**
	 * @param  string|array<string> $types
	 * @param  string? $name
	 * @param  string? $scope
	 * @param  callable? $provider
	 * @throws InvalidArgumentException
	 */
	public function __construct($types, $name = null, $scope = null, $provider = null)
	{
		if (!$this->constructed)
		{
			parent::__construct($types, $name, $scope);
			
			$this->constructed = true;
		}
		else
		{
			$this->getDefinition()->setArguments(func_get_args());
		}
	}
	
	/**
	 * @return ObjectDefinition
	 */
	public function getDefinition()
	{
		if ($this->definition === null)
		{
			$types = $this->getTypes();
			
			$this->definition = new ObjectDefinition($types[0]);
		}
		
		return $this->definition;
	}
	
	/**
	 * @throws BadMethodCallException
	 */
	public function __isset($name)
	{
		throw new BadMethodCallException();
	}
	
	/**
	 * @throws BadMethodCallException
	 */
	public function __get($name)
	{
		throw new BadMethodCallException();
	}
	
	/**
	 * @param  string $name
	 * @param  mixed $value
	 */
	public function __set($name, $value)
	{
		$this->getDefinition()->setField($name, $value);
	}
	
	/**
	 * @throws BadMethodCallException
	 */
	public function __unset($name)
	{
		throw new BadMethodCallException();
	}
	
	/**
	 * @param  string $method
	 * @param  array $arguments
	 */
	public function __call($method, array $arguments)
	{
		if ($method === '__construct')
		{
			$this->getDefinition()->setArguments($arguments);
		}
		else
		{
			$this->getDefinition()->addMethodCall(new MethodCall($method, $arguments));
		}
	}
	
	/**
	 * @throws BadMethodCallException
	 */
	public function offsetExists($offset)
	{
		throw new BadMethodCallException();
	}
	
	/**
	 * @throws BadMethodCallException
	 */
	public function offsetGet($offset)
	{
		throw new BadMethodCallException();
	}

	/**
	 * @param  mixed $offset
	 * @param  mixed $value
	 */
	public function offsetSet($offset, $value)
	{
		$this->getDefinition()->addMethodCall(new MethodCall('offsetSet', array($offset, $value)));
	}

	/**
	 * @throws BadMethodCallException
	 */
	public function offsetUnset($offset)
	{
		throw new BadMethodCallException();
	}
}